package com.itheima.reggie.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.ICategoryService;
import com.itheima.reggie.service.IDishFlavorService;
import com.itheima.reggie.service.IDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    IDishService dishService;

    @Autowired
    ICategoryService categoryService;

    @Autowired
    IDishFlavorService dishFlavorService;

    /**
     * 分页+条件查询菜品接口
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        //用于查询dish分页数据de
        Page<Dish> dishPage = new Page<Dish>(page, pageSize);


        //构造条件构造器
        LambdaQueryWrapper<Dish> dishqueryWrapper = new LambdaQueryWrapper<>();
        //设置条件查询
        dishqueryWrapper.like(!StringUtils.isEmpty(name), Dish::getName, name);
        //添加排序条件
        dishqueryWrapper.orderByDesc(Dish::getCreateTime);

        dishService.page(dishPage, dishqueryWrapper);

        //最终返回给前端的
        Page<DishDto> dishDtoPage = new Page<DishDto>();
        //实现Page<DishDto>和Page<Dish>之间属性的对拷
        BeanUtils.copyProperties(dishPage, dishDtoPage, "records");
        //写法一：for循环

        //写法二：map接口
        List<DishDto> dishDtoList = dishPage.getRecords().stream().map(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            Category category = categoryService.getById(dish.getCategoryId());
            dishDto.setCategoryName(category.getName());
            return dishDto;
        }).collect(Collectors.toList());


        dishDtoPage.setRecords(dishDtoList);
        return R.success(dishDtoPage);

    }


    /**
     * 新增菜品
     * 表现层接收待添加的菜品数据时，需要使用dishDto来接受
     * 不然 dish是拿不到菜品口味数据的
     */
    @PostMapping
    public R save(@RequestBody DishDto dishDto) {

        dishService.saveWithFlavor(dishDto);

        return R.success("新增菜品成功");
    }

    /**
     * 根据id来查询菜品信息和对应的口味信息
     */
    @GetMapping("/{id}")
    public R<DishDto> updateid(@PathVariable Long id) {
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto) {
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }

    /**
     * 删除菜品与批量删除菜品
     */
    @DeleteMapping
    public R deleteDish(@RequestParam List<Long> ids) {
        //逻辑删除，实际进行更新
        dishService.removeByIds(ids);
        //delete from dish where id in{ids}
        return R.success("删除成功");
    }

    /**
     * 修改菜品状态与批量修改菜品状态
     * 批量更新
     */
    @PostMapping("/status/{status}") // 多了一个小括号）
    public R<String> changeStatus(@PathVariable Integer status,@RequestParam List<Long> ids) {

     //构造条件构造器
        LambdaUpdateWrapper<Dish> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.set(Dish::getStatus,status);
        queryWrapper.in(Dish::getId, ids);
        dishService.update(queryWrapper);

        return R.success("修改成功");
    }


}
